﻿<%@ Application Language="C#" %>
<%@ Import Namespace="BLL" %>

<script runat="server">

    void Application_BeginRequest(object sender, EventArgs e)
    {
        string url = Request.Url.ToString().ToLower();
        //Khoi tao rule
        UrlRewritingNet.Web.RegExRewriteRule ruleCategory = new UrlRewritingNet.Web.RegExRewriteRule();
        UrlRewritingNet.Web.RegExRewriteRule ruleProduct = new UrlRewritingNet.Web.RegExRewriteRule();
        UrlRewritingNet.Web.RegExRewriteRule ruleAlbum_Categories = new UrlRewritingNet.Web.RegExRewriteRule();
        UrlRewritingNet.Web.RegExRewriteRule ruleAlbum_Products = new UrlRewritingNet.Web.RegExRewriteRule();
        UrlRewritingNet.Web.RegExRewriteRule ruleArticle_Categories = new UrlRewritingNet.Web.RegExRewriteRule();
        UrlRewritingNet.Web.RegExRewriteRule ruleArticle_Products = new UrlRewritingNet.Web.RegExRewriteRule();

        ////Rule – rewrite cho Categories
        //ruleCategory.VirtualUrl = "^~/Categories/(.*)/(.*)/(.*).aspx";
        //ruleCategory.DestinationUrl = "~/BrowseProducts.aspx?Dep1ID=$1&DepID=$2";      
        //ruleCategory.IgnoreCase = true;
        //ruleCategory.Rewrite = UrlRewritingNet.Web.RewriteOption.Application;
        //ruleCategory.Redirect = UrlRewritingNet.Web.RedirectOption.None;
        //ruleCategory.RewriteUrlParameter = UrlRewritingNet.Web.RewriteUrlParameterOption.ExcludeFromClientQueryString;
        //UrlRewritingNet.Web.UrlRewriting.AddRewriteRule("Category_Rule", ruleCategory);

        //Rule – rewrite cho Categorie
        ruleCategory.VirtualUrl = "^~/category/(.*)_(.*)";
        ruleCategory.DestinationUrl = "~/BrowseProducts.aspx?DepID=$1";
        ruleCategory.IgnoreCase = true;
        ruleCategory.Rewrite = UrlRewritingNet.Web.RewriteOption.Application;
        ruleCategory.Redirect = UrlRewritingNet.Web.RedirectOption.None;
        ruleCategory.RewriteUrlParameter = UrlRewritingNet.Web.RewriteUrlParameterOption.ExcludeFromClientQueryString;
        UrlRewritingNet.Web.UrlRewriting.AddRewriteRule("Category_Rule", ruleCategory);
        
        //Rule – rewrite cho Products
        ruleProduct.VirtualUrl = "^~/Products/(.*)/(.*).aspx";
        ruleProduct.DestinationUrl = "~/ShowProduct.aspx?ID=$1";
        ruleProduct.IgnoreCase = true;
        ruleProduct.Rewrite = UrlRewritingNet.Web.RewriteOption.Application;
        ruleProduct.Redirect = UrlRewritingNet.Web.RedirectOption.None;
        ruleProduct.RewriteUrlParameter = UrlRewritingNet.Web.RewriteUrlParameterOption.ExcludeFromClientQueryString;
        UrlRewritingNet.Web.UrlRewriting.AddRewriteRule("Product_Rule", ruleProduct);

        //Rule – rewrite cho Categories_Album
        ruleAlbum_Categories.VirtualUrl = "^~/hinh-anh/(.*)/(.*).aspx";
        ruleAlbum_Categories.DestinationUrl = "~/BrowseAlbums.aspx?DeptID=$1";
        ruleAlbum_Categories.IgnoreCase = true;
        ruleAlbum_Categories.Rewrite = UrlRewritingNet.Web.RewriteOption.Application;
        ruleAlbum_Categories.Redirect = UrlRewritingNet.Web.RedirectOption.None;
        ruleAlbum_Categories.RewriteUrlParameter = UrlRewritingNet.Web.RewriteUrlParameterOption.ExcludeFromClientQueryString;
        UrlRewritingNet.Web.UrlRewriting.AddRewriteRule("Thuvien_Rule", ruleAlbum_Categories);

        //Rule – rewrite cho Categories_Album
        ruleAlbum_Products.VirtualUrl = "^~/album/(.*)/(.*).aspx";
        ruleAlbum_Products.DestinationUrl = "~/ShowAlbum.aspx?ID=$1";
        ruleAlbum_Products.IgnoreCase = true;
        ruleAlbum_Products.Rewrite = UrlRewritingNet.Web.RewriteOption.Application;
        ruleAlbum_Products.Redirect = UrlRewritingNet.Web.RedirectOption.None;
        ruleAlbum_Products.RewriteUrlParameter = UrlRewritingNet.Web.RewriteUrlParameterOption.ExcludeFromClientQueryString;
        UrlRewritingNet.Web.UrlRewriting.AddRewriteRule("Thuvien_Rule", ruleAlbum_Products);

        //Rule – rewrite cho ruleArticle_Categories
        ruleArticle_Categories.VirtualUrl = "^~/bai-viet/(.*)/(.*).aspx";
        ruleArticle_Categories.DestinationUrl = "~/BrowseArticles.aspx?DeptID=$1";
        ruleArticle_Categories.IgnoreCase = true;
        ruleArticle_Categories.Rewrite = UrlRewritingNet.Web.RewriteOption.Application;
        ruleArticle_Categories.Redirect = UrlRewritingNet.Web.RedirectOption.None;
        ruleArticle_Categories.RewriteUrlParameter = UrlRewritingNet.Web.RewriteUrlParameterOption.ExcludeFromClientQueryString;
        UrlRewritingNet.Web.UrlRewriting.AddRewriteRule("Thuvien_Rule", ruleArticle_Categories);

        //Rule – rewrite cho Categories_Album
        ruleArticle_Products.VirtualUrl = "^~/thoi-trang/(.*)/(.*).aspx";
        ruleArticle_Products.DestinationUrl = "~/ShowArticle.aspx?ID=$1";
        ruleArticle_Products.IgnoreCase = true;
        ruleArticle_Products.Rewrite = UrlRewritingNet.Web.RewriteOption.Application;
        ruleArticle_Products.Redirect = UrlRewritingNet.Web.RedirectOption.None;
        ruleArticle_Products.RewriteUrlParameter = UrlRewritingNet.Web.RewriteUrlParameterOption.ExcludeFromClientQueryString;
        UrlRewritingNet.Web.UrlRewriting.AddRewriteRule("Thuvien_Rule", ruleArticle_Products);         
    }
    
    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup

        Application["VisitorOnline"] = 0;
        Application["NumVisitor"] = Helpers.ReadFile();
        Application["FCKeditor:UserFilesPath"] = "~/userfiles/";
        //------------------------
        Application["HomNay"] = 0;
        Application["HomQua"] = 0;
        Application["TuanNay"] = 0;
        Application["TuanTruoc"] = 0;
        Application["ThangNay"] = 0;
        Application["ThangTruoc"] = 0;
        Application["TatCa"] = 0;
        Application["visitors_online"] = 0;
        //------------------------
    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started

        Application["NumVisitor"] =Helpers.ReadFile();
        Application["NumVisitor"] = int.Parse(Application["NumVisitor"].ToString()) + 1;
        Application["VisitorOnline"] = int.Parse(Application["VisitorOnline"].ToString()) + 1;
        Helpers.WriteFile(int.Parse(Application["NumVisitor"].ToString()));
        //-------------------------
        Session.Timeout = 150;
        Application.Lock();
        Application["visitors_online"] = Convert.ToInt32(Application["visitors_online"]) + 1;
        Application.UnLock();
        try
        {
            System.Data.DataTable dtb = Helpers.HitCounter();
            if (dtb.Rows.Count > 0)
            {
                Application["HomNay"] = long.Parse("0" + dtb.Rows[0]["HomNay"]).ToString("#,###");
                Application["HomQua"] = long.Parse("0" + dtb.Rows[0]["HomQua"]).ToString("#,###");
                Application["TuanNay"] = long.Parse("0" + dtb.Rows[0]["TuanNay"]).ToString("#,###");
                Application["TuanTruoc"] = long.Parse("0" + dtb.Rows[0]["TuanTruoc"]).ToString("#,###");
                Application["ThangNay"] = long.Parse("0" + dtb.Rows[0]["ThangNay"]).ToString("#,###");
                Application["ThangTruoc"] = long.Parse("0" + dtb.Rows[0]["ThangTruoc"]).ToString("#,###");
                Application["TatCa"] = long.Parse("0" + dtb.Rows[0]["TatCa"]).ToString("#,###");
            }
            dtb.Dispose();
        }
        catch { }
        //-------------------------
    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.
        Application["VisitorOnline"] = int.Parse(Application["VisitorOnline"].ToString()) - 1;
        //---------------------------
        Application.Lock();
        Application["visitors_online"] = Convert.ToUInt32(Application["visitors_online"]) - 1;
        Application.UnLock();
        //---------------------------
    }
       
</script>
